const fetch = require('node-fetch');
// fetch的请求参数

// 2.定制请求
async function fetchText (params) {
  // fetch的第二个参数就是请求头对象
  let response = await fetch('https://api.github.com/users/ruanyf', {
    method: 'POST',
    headers: {
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    },
    body: 'foo=bar&lorem=ipsum',
  })

  console.log(await response.text());//获取文本

  // 提交json参数
  /*
  const user = { name: 'John', surname: 'Smith' };
   const response = await fetch('/article/fetch/post/user', {
     method: 'POST',
     headers: {
       'Content-Type': 'application/json;charset=utf-8'
     },
     body: JSON.stringify(user)
   });
  */
  //  提交form参数
  /*
  const form = document.querySelector('form');

  const response = await fetch('/users', {
    method: 'POST',
    body: new FormData(form)
  })
  */
  //  完整API
  /*
    const response = fetch(url, {
      method: "GET",
      headers: {
      "Content-Type": "text/plain;charset=UTF-8"
      },
      body: undefined,
      referrer: "about:client",
      referrerPolicy: "no-referrer-when-downgrade",
      mode: "cors", 
      credentials: "same-origin",
      cache: "default",
      redirect: "follow",
      integrity: "",
      keepalive: false,
      signal: undefined
    });
  */
}
fetchText();